/**
 * @(#) InrecMediator.as
 *
 * @date		2012. 11. 07.
 * @version		1.0
 * @author		Park, Jutae
 * @description	사내추천신청 - Mediator
 * 				Inrec를 조회하고 신청하는 Presentation Logic을 관리 
 */
package com.skt.common_hr.inrec.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.skt.common_hr.inrec.InrecFacade;
	import com.skt.common_hr.inrec.events.ComboBoxYearEvent;
	import com.skt.common_hr.inrec.model.InrecProxy;
	import com.skt.common_hr.inrec.model.vo.InrecVO;
	import com.skt.common_hr.inrec.view.components.InrecView;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.TextInput;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class InrecMediator extends OverViewMediator
	{
		public static const NAME:String = "InrecMediator"; 


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function InrecMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		
		override protected function initializeView():void
 		{
			super.initializeView();
			loadYear();
 		}
		
 		override protected function activatedView():void
 		{
 			setEventListeners();
 			loadList();
 		} 
 		
 		override protected function closedView():void
 		{
			removeEventListeners(); 			
 		}
 		
 		private function setEventListeners():void
 		{
 			view.addEventListener(InrecView.Insert, onSinChung);
 			view.addEventListener(InrecView.Update, onUpdate);
 			view.addEventListener(InrecView.Delete, onDelete);
 			view.addEventListener(InrecView.dgClick, onRead);
 			view.addEventListener(InrecView.DOWN_DATA, onStatementViewAttach);
 			view.addEventListener(ComboBoxYearEvent.SELECT_YEAR_DATA, onComboYear);
 		}
 		
 		private function removeEventListeners():void
 		{
 			view.removeEventListener(InrecView.Insert, onSinChung);
 			view.removeEventListener(InrecView.Update, onUpdate);
 			view.removeEventListener(InrecView.Delete, onDelete);
 			view.removeEventListener(InrecView.dgClick, onRead);
 			view.removeEventListener(InrecView.DOWN_DATA, onStatementViewAttach);
 		}
 		
 		public function loadYear():void
 		{
 			var proxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
 			proxy.loadYear(Variables.myPernr);
 		}
 		
 		/* 사내추천 클릭시 List 조회 */
 		private function loadList():void 
 		{
 			view.vs.selectedIndex = 0;
 			view.cbYear.selectedIndex = 0;
 			
 			var proxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
 			proxy.load(Variables.myPernr);
 		}
 		
 		/* 사내추천 신청 클릭시 */
 		private function onSinChung(e:Event):void
 		{
 			executeReqView()
 		}
 		
 		private function executeReqView(param:Object=null):void
 		{
 			view.reqView.sDvPop = view.sDv;
 			view.vs.selectedIndex = 1;
 			var reqMediator:ReqInrecMediator = facade.retrieveMediator(ReqInrecMediator.NAME) as ReqInrecMediator;
				
 			if(!reqMediator) {
				reqMediator = new ReqInrecMediator(view.reqView);
	 			facade.registerMediator(reqMediator);
			}
 			if(reqMediator) facade.sendNotification(InrecFacade.LOAD_CODES_COMPLETE, param);
 		}
 		
 		/* 사내추천 수정 클릭시 */
 		private function onUpdate(e:Event):void
 		{
 			
 			view.vs.selectedIndex = 1;
 			view.reqView.vb.removeAllChildren();
 			
 			var obj:InrecVO = view.dg.selectedItem as InrecVO;
			
			var ac:ArrayCollection = e.target.causeList as ArrayCollection;
			var ac2:ArrayCollection = new ArrayCollection();
			for(var i:int=0; i<ac.length; i++) {
				var selectedItem:Object = ac.getItemAt(i);
				if(obj.SRTFD == selectedItem.SRTFD){
					ac2.addItem(selectedItem);
				}
			}
			
			var updateList:Object = new Object();
			updateList.oSave = obj;
			updateList.oSave2 = ac2;
			view.reqView.sDvPop = view.sDv;
 			
 			executeModifyView(updateList);
 		}
 		
 		private function executeModifyView(param:Object):void
		{
 			
 			var reqMediator:ReqInrecMediator = facade.retrieveMediator(ReqInrecMediator.NAME) as ReqInrecMediator;
				
 			if(!reqMediator) {
				reqMediator = new ReqInrecMediator(view.reqView);
	 			facade.registerMediator(reqMediator);
			}
			
 			if(reqMediator) facade.sendNotification(InrecFacade.UPDATE_COMPLETE, param);
		}
		
		/* 사내추천 조회 클릭시 */
		private function onRead(event:Event):void
		{
			var _vo:InrecVO = new InrecVO();
 				_vo.GJAHR =  view.cbYear.selectedItem.data;
			
			var proxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
 			proxy.readLoad(Variables.myPernr, _vo);
		}
		
		/* 사내추천 삭제 클릭시 */
		private function onDelete(event:Event):void
		{
			var obj:InrecVO = view.dg.selectedItem as InrecVO;
			
 			var proxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
 			proxy.del(Variables.myPernr, obj); 
			
		}
		
		/* 사내추천 다운로드 클릭시 */
		private function onStatementViewAttach(event:Event):void
 		{
 			var obj:InrecVO = view.dg.selectedItem as InrecVO;
 			
 			var statementProxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
			statementProxy.loadAttach(Variables.myPernr, obj);
 		}
 		
 		/* 메인화면 대상년도 변경시 */
 		private function onComboYear(event:ComboBoxYearEvent):void
 		{
 			var cbProxy:InrecProxy = facade.retrieveProxy(InrecProxy.NAME) as InrecProxy;
			cbProxy.load(Variables.myPernr, event.comData);
 		}
 		
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					InrecFacade.LOAD_LIST_COMPLETE,
					InrecFacade.LOAD_LIST_FAILED,
					InrecFacade.UPDATE_COMPLETE,
					InrecFacade.UPDATE_FAILED,
					InrecFacade.DELETE_COMPLETE,
					InrecFacade.DELETE_FAILED,
					InrecFacade.READ_LIST_COMPLETE,
					InrecFacade.READ_LIST_FAILED,
					InrecFacade.LOAD_YEAR_COMPLETE,
					InrecFacade.LOAD_YEAR_FAILED,
					InrecFacade.OK_REQUEST_INPUT,
					InrecFacade.CANCEL_REQUEST_INPUT,
					InrecProxy.LOAD_STATEMENT_URL_COMPLTE,
					InrecProxy.LOAD_STATEMENT_URL_FAILED
					]);
		}
		
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case InrecFacade.LOAD_YEAR_COMPLETE:
					onLoadYearList(note.getBody() as InrecProxy);
					break;
				case InrecFacade.LOAD_YEAR_FAILED:
					break;
				case InrecFacade.LOAD_LIST_COMPLETE:
					onLoadList(note.getBody() as InrecProxy);
					break;
				case InrecFacade.LOAD_LIST_FAILED:
					break;
				case InrecFacade.READ_LIST_COMPLETE:
					onReadList(note.getBody() as InrecProxy);
					break;
				case InrecFacade.READ_LIST_FAILED:
					break;	
				case InrecFacade.OK_REQUEST_INPUT:
					view.vs.selectedIndex = 0;
					loadList();
					break;
				case InrecFacade.CANCEL_REQUEST_INPUT:
					Alert.show((InrecProxy(note.getBody()).failedAlert));
					break;
				case InrecFacade.DELETE_COMPLETE:
					loadList();
					break;
				case InrecProxy.LOAD_STATEMENT_URL_COMPLTE:
					onNotePrint((note.getBody() as InrecProxy).url);
					break;
				case InrecProxy.LOAD_STATEMENT_URL_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "("+(note.getBody() as InrecProxy).errCode+")"+ (note.getBody() as InrecProxy).errText);
					break;
				default:
					break;
			}
		}
		
		private function onLoadYearList(proxy:InrecProxy):void
 		{
 			view.acMedYear = proxy.acMedYear;
 			loadList();
 		}
		
 		private function onLoadList(proxy:InrecProxy):void
 		{
 			view.inrecColl = proxy.inrecList;
 			view.acMedYear = proxy.acMedYear;
 			view.causeList = proxy.causeList;
 			
 			var vo:InrecVO =proxy.sinChungList.getItemAt(0) as InrecVO; 
 			
 			view.reqView.lbORGTX.text = vo.ORGTX;
 			view.reqView.lbPERNR.text = vo.PERNR.substring(1,8);
 			view.reqView.lbENAME.text = vo.ENAME;
 			view.reqView.lbPTEXT.text = vo.PTEXT;
 			view.reqView.lbINTEL.text = vo.INTEL;
 			view.reqView.lbAPPSTT.text = vo.APPSTT;
 			view.reqView.lbAPPRET.text = vo.APPRET;
 			
 			
 			view.reqView.btnSave.visible = true;
 			
 		}
 		
 		private function onReadList(proxy:InrecProxy):void
 		{
 			view.vs.selectedIndex = 1;
 			
 			var vo:InrecVO = view.dg.selectedItem as InrecVO;
 			var ac:ArrayCollection = proxy.readList2; 
			var ac2:ArrayCollection = new ArrayCollection();
			for(var i:int=0; i<ac.length; i++) {
				var selectedItem:Object = ac.getItemAt(i);
				if(vo.SRTFD == selectedItem.SRTFD){
					ac2.addItem(selectedItem);
				}
			} 
			
 			view.reqView.sDvPop = view.sDv;
 			
 			view.reqView.tiAPPNM.text 	= vo.APPNM;
			view.reqView.tiAPPTE.text 	= vo.APPTE;
			view.reqView.tiAPPOR.text 	= vo.APPOR;
			view.reqView.df.text	  	= vo.APPBR.substring(0, 10);
			view.reqView.lbAPPSTT.text  = vo.APPSTT;
			view.reqView.lbAPPRET.text = vo.APPRET;
			view.reqView.tiAPPNM.enabled = false;
			view.reqView.tiAPPTE.enabled = false; 
			view.reqView.tiAPPOR.enabled = false;
			view.reqView.df.enabled = false;
			view.reqView.tiLine.enabled = false;
			view.reqView.btnSave.visible = false;
			
			view.reqView.vb.removeAllChildren();
			view.reqView.vb.enabled = true;
			view.reqView.vb.verticalScrollPosition = 0;
			
			addReadTextInput(ac2);
 		}
 		
 		public function addReadTextInput(ac:ArrayCollection):void
 		{
 			var selectedItem:Object; 
 			for(var i:int=0; i<ac.length; i++){
 				selectedItem = ac.getItemAt(i);
				var tiTextInput:TextInput =  new TextInput();
				tiTextInput.percentWidth = 100;
				tiTextInput.maxChars = 40;
				tiTextInput.setStyle('borderStyle', 'solid');
				tiTextInput.setStyle('borderSides', 'bottom');
				tiTextInput.setStyle('focusThickness', '0');
				tiTextInput.setStyle('color', 'black');
				tiTextInput.editable = false;
				view.reqView.vb.addChildAt(tiTextInput, i);
				tiTextInput.addEventListener(KeyboardEvent.KEY_DOWN, view.reqView.keyDownHandlers);
				tiTextInput.addEventListener(KeyboardEvent.KEY_UP, view.reqView.keyHandler);
				tiTextInput.text = selectedItem.TLINE;
				
 			}
 		}
 		 
		private function onNotePrint(url:String):void
		{ 
			var req:URLRequest = new URLRequest("../ace_inrec/"+url);
			navigateToURL(req);
		}
 		
        private function get view():InrecView
        {
            return viewComponent as InrecView;
        }
		
		
	}
}